package com.meeting.meetingroom_api.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.meeting.meetingroom_api.entity.User; // 假设您的User实体类路径
import com.meeting.meetingroom_api.enums.AccountStatus; // 假设您的AccountStatus枚举路径
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class UserDetailsImpl implements UserDetails {
    private static final long serialVersionUID = 1L;

    private Integer id;
    private String username;
    @JsonIgnore
    private String password;
    private Collection<? extends GrantedAuthority> authorities;
    private AccountStatus accountStatus; // Store original account status

    public UserDetailsImpl(Integer id, String username, String password,
                           Collection<? extends GrantedAuthority> authorities, AccountStatus accountStatus) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.authorities = authorities;
        this.accountStatus = accountStatus;
    }

    public static UserDetailsImpl build(User user) {
        List<GrantedAuthority> authorities = Collections.singletonList(
                new SimpleGrantedAuthority("ROLE_" + user.getRole().name().toUpperCase()) // 假设 UserRole 枚举有 .name() 方法
        );

        return new UserDetailsImpl(
                user.getUserId(), // 假设 User 实体有 getId() 或 getUserId()
                user.getUsername(),
                user.getPasswordHash(), // 假设 User 实体有 getPasswordHash()
                authorities,
                user.getAccountStatus() // 假设 User 实体有 getAccountStatus()
        );
    }

    public Integer getId() {
        return id;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true; // 根据您的业务逻辑调整
    }

    @Override
    public boolean isAccountNonLocked() {
        // 例如：如果账户状态不是 FROZEN 或 PENDING_APPROVAL
        return accountStatus != AccountStatus.frozen && accountStatus != AccountStatus.pending_approval;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true; // 根据您的业务逻辑调整
    }

    @Override
    public boolean isEnabled() {
        return accountStatus == AccountStatus.active;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserDetailsImpl user = (UserDetailsImpl) o;
        return Objects.equals(id, user.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}